//.filter-wrapper 更多按钮收起、或者展开
var $wrapper = $('.filter-wrapper'),
    $btn = $('.revision-btn-search', $wrapper);
$('a.more').on('click', function () {
    if ($wrapper.hasClass('open')) {
        $(this).html(
            '更多<i class="revision-icon-more"></i>'
        );
        $wrapper.removeClass('open').css({
            'height': 34
        });
        $btn.css({ 'bottom': '2px' });
    } else {
        $(this).html(
            '收起<i class="revision-icon-more active"></i>'
        );
        $wrapper.addClass('open').css({
            'height': $('ul', $wrapper).height()
        });
        $btn.css({ 'bottom': '4px' });
    }
});
window.onresize = function() {
    // select2
    if (document.body.clientWidth > 1660) {
        $('.filter-wrapper .select2.select2-container.select2-container--default').css('width', '220px');
    } else {
        $('.filter-wrapper .select2.select2-container.select2-container--default').css('width', '180px');
    }
};
//tab(弹出层需要单页面内写)
(function (pluginName) {
    $.fn[pluginName] = function (param) {
        var arr = [],
            len = this.length;
        for (var i = 0; i < len; i++) {
            var obj = new Tab(this[i], param);
            arr.push(obj);
        }
        return arr;
    }

    function Tab(elem, param) {
        var defaults = {
            tabEvent: 'click',
            elem: $(elem),
            curClass: 'current', // 当前选项的类名
            beforeTab: null, // 切换之前的回调
            fn: null // 切换(之后)回调
        };
        $.extend(this, defaults, param);
        this.init();
    }

    Tab.prototype = {
        init: function () {
            var _this = this;
            $('li', _this.elem.children('.tabs')).on(_this.tabEvent, function () {
                var $this = $(this),
                    index = 0,
                    oriIndex = -1; // 切换之前的索引
                if ($this.hasClass(_this.curClass)) return;
                index = $this.index();
                oriIndex = $('.tabs .' + _this.curClass, _this.elem).index();
                _this.openTab(index, oriIndex);
            });
        },
        openTab: function (index, oriIndex) {
            debugger;
            if (typeof this.beforeTab === 'function' && typeof oriIndex != 'undefined') {
                var flag = this.beforeTab(index, oriIndex);
                if (flag === false) {
                    return;
                }
            }
            $('li', this.elem.children('.tabs')).eq(index).addClass(this.curClass).siblings().removeClass(this.curClass);
            this.elem.children('.tab-cont').children('.tab-item').eq(index).removeClass('none').siblings().addClass('none');
            if (typeof this.fn === 'function') {
                this.fn(index);
            }
        }
    }
})('tab');
var baseData = {
  //遮罩加载条
  loading: false,
  //列表加载条
  listLoading: false,
  pageInit: true,
};
//获取基础数据方法(可放在localStrange)
var baseCommonFunction = {
    //url 地址 sendData数据 type:POST,GET  callBack:回调
    PageAajx:function(url,sendData,beforeSend,type,callBack){
        var page={
            Page: vm.$get("pageObj") === undefined ? 1 : vm.$get("pageObj.pageNumber"),
            PageSize: vm.$get("pageObj") === undefined ? 10 : vm.$get("pageObj.pageSize")
        };
        $.ajax({
            type: type || 'POST',
            url: url,
            data: sendData,
            dataType: "json",
            beforeSend: beforeSend || function () {
                if (vm !== undefined) {
                    vm.$set('listLoading', true);
                    vm.$set('pageObj.dataTotal', 0);
                    vm.$set('pageObj.pageNumber', 0);
                    vm.$set('result.List', []);
                    vm.$set('result.Summary', {});
                    vm.$set("ajaxLoad", true);
                }
            },
            success: success || function (data) {
                vm.$set('listLoading', false);
                if (data.Success) {
                    vm.$set('result.List', data.Data.ViewModel.Models);
                    vm.$set('result.Summary', data.Data.Summary);
                    vm.$set('result.Extra', data.Data.Extra);
                    vm.$set('pageObj.dataTotal', data.Data.ViewModel.RecordCount);
                    vm.$set('pageObj.pageNumber', data.Data.ViewModel.PageIndex);
                    if (scanType === "scanninggun") {
                        if (data.Data.ViewModel.Models.length > 0) {
                            vm.showApproval(data.Data.ViewModel.Models[0], data.Data.ViewModel.Models[0].ApprovalStatus);
                        }
                    }
                } else {
                    vm.$set('result.List', []);
                    vm.$set('result.Summary', {});
                    vm.$set('result.Extra', {});
                    vm.$set('pageObj.dataTotal', 0);
                    vm.$set('pageObj.pageNumber', 0);
                    vm.Alert(data.Message == "" ? "查询失败，" : data.Message);
                }
                vm.$set("ajaxLoad", false);
                if (callBack != undefined && callBack != null) {
                    callBack(data);
                }
            },
            error: error || function () {
                vm.$set('result.List', []);
                vm.$set('result.Summary', {});
                vm.$set('result.Extra', {});
                vm.$set('listLoading', false);
                vm.$set("ajaxLoad", false);
            }
        });
    },
    //isAll 是否要返回全部树 0 全部 1部分（带权限）
    //isShowInvalidation 是否显示无效 underfind/0：显示全部 1：不显示无效
    loadTree: function ( isAll, isShowInvalidation) {
        //ajax请求数据格式
        var dataObj=[
            {
                "treePath": "/",
                "id": "300000",
                "text": "all",
                "value": "300000",
                "remain": "True",
                "ChildNodes": [
                    {
                        "treePath": "1",
                        "hasChildren": true,
                        "id": "6870",
                        "text": "a",
                        "value": "6870",
                        "remain": "False",
                        "ChildNodes":
                        [
                            {
                                "treePath": "2",
                                "hasChildren": true,
                                "id": "7124",
                                "text": "b",
                                "value": "7124",
                                "remain": "False"
                            },
                            {
                                "treePath": "2",
                                "hasChildren": true,
                                "id": "6982",
                                "text": "c",
                                "value": "6982",
                                "remain": "False",
                                "ChildNodes": [
                                    {
                                        "treePath": "3",
                                        "hasChildren": true,
                                        "id": "7159",
                                        "text": "dbc",
                                        "value": "7159",
                                        "remain": "False",
                                        "ChildNodes": [
                                            {
                                                "treePath": "4",
                                                "hasChildren": true,
                                                "id": "8285",
                                                "text": "e",
                                                "value": "8285",
                                                "remain": "False"
                                            },
                                            {
                                                "treePath": "4",
                                                "hasChildren": true,
                                                "id": "8753",
                                                "text": "fa",
                                                "value": "8753",
                                                "remain": "False"
                                            },
                                            {
                                                "treePath": "4",
                                                "hasChildren": true,
                                                "id": "8872",
                                                "text": "g",
                                                "value": "8872",
                                                "remain": "False"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
        if (dataObj) {
            var profitTreeObj = $.fn.zTree.init($('#productTree'), {
                check: {
                    enable: true
                },
                data: {
                    key: {
                        name: 'text',
                        children: 'ChildNodes',
                        title: 'text'
                    }
                },
                callback: {
                    onCheck: function () {
                        vm.setCheckId(profitTreeObj.getCheckedNodes(), "treeIds");
                    }
                }
            }, dataObj);
            profitTreeObj.checkAllNodes(true);
            vm.setCheckId(profitTreeObj.getCheckedNodes(), "treeIds");
            vm.treeIds.paramId = vm.treeIds.checkedId;
        }
    }
    
};
//参数格式化
var mvcParamMatch = (function () {
    //格式转换
    var MvcParameterAdaptive = {};
    //验证是否为数组
    MvcParameterAdaptive.isArray = Function.isArray || function (o) {
        return typeof o === "object" &&
            Object.prototype.toString.call(o) === "[object Array]";
    };
    //将数组转换为对象
    MvcParameterAdaptive.convertArrayToObject = function ( /*数组名*/ arrName, /*待转换的数组*/ array, /*转换后存放的对象，不用输入*/ saveOjb) {
        var obj = saveOjb || {};
        function func(name, arr) {
            for (var i in arr) {
                if (!MvcParameterAdaptive.isArray(arr[i]) && typeof arr[i] === "object") {
                    for (var j in arr[i]) {
                        if (MvcParameterAdaptive.isArray(arr[i][j])) {
                            func(name + "[" + i + "]." + j, arr[i][j]);
                        } else if (typeof arr[i][j] === "object") {
                            MvcParameterAdaptive.convertObject(name + "[" + i + "]." + j + ".", arr[i][j], obj);
                        } else {
                            obj[name + "[" + i + "]." + j] = arr[i][j];
                        }
                    }
                } else {
                    obj[name + "[" + i + "]"] = arr[i];
                }
            }
        }

        func(arrName, array);

        return obj;
    };
    //转换对象
    MvcParameterAdaptive.convertObject = function ( /*对象名*/ objName, /*待转换的对象*/ turnObj, /*转换后存放的对象，不用输入*/ saveOjb) {
        var obj = saveOjb || {};
        function func(name, tobj) {
            for (var i in tobj) {
                if (MvcParameterAdaptive.isArray(tobj[i])) {
                    MvcParameterAdaptive.convertArrayToObject(name + i, tobj[i], obj); //将数组所属的对象名称也传递过去
                } else if (typeof tobj[i] === "object") {
                    func(name + i + ".", tobj[i]);
                } else {
                    obj[name + i] = tobj[i];
                }
            }
        }
        func(objName, turnObj);
        return obj;
    };
    return function (json, arrName) {

        arrName = arrName || "";
        if (typeof json !== "object") throw new Error("请传入json对象");
        if (MvcParameterAdaptive.isArray(json) && !arrName) throw new Error("请指定数组名，对应Action中数组参数名称！");

        if (MvcParameterAdaptive.isArray(json)) {
            return MvcParameterAdaptive.convertArrayToObject(arrName, json);
        }
        return MvcParameterAdaptive.convertObject("", json);
    };
})();


